package com.wider.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wider.foundation.nxfile.NxFS;

/**
 * Servlet implementation class NxServlet
 */
public class NxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NxServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String command = request.getParameter("COMMAND CLASS");
		/*
		if (!command.equals(NxFS.class.toString())){
			response.sendError(response.SC_METHOD_NOT_ALLOWED);
			return;
		}
		
		command = request.getParameter("COMMAND STRING");
		if (command.equals("GET FILE INFO")){
			long filecount = Long.parseLong(request.getParameter("ARGUMENT COUNT"));

			JSONArray json_files = new JSONArray();
			try{
				for (long i = 0; i < filecount; i++){
					JSONObject json_finfo = new JSONObject();
					File file = (File) request.getpa
					json_finfo.put("FILE PATH", request.getParameter("FILE PATH"));
					json_finfo.put("FILE EXISTS", )
					
				}
				
				JSONObject json = new JSONObject();
				json.put("COMMAND CLASS", NxFS.class.toString());
				json.put("COMMAND STRING", command);
			}catch(JSONException e){
				e.printStackTrace();
				response.sendError(response.SC_INTERNAL_SERVER_ERROR);
			}
		}
		*/
		PrintWriter rspndr = response.getWriter();
		rspndr.println("Query: " + request.getRequestURI());
		Map<String, Object> paramap = request.getParameterMap();
		for (Map.Entry<String, Object> e : paramap.entrySet()){
			String key = e.getKey();
			List<String> val = Arrays.asList(request.getParameterValues(key));
			for (String v : val){
				rspndr.println("Key: " + key + ", Val: " + v);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
